package com.codesuc.aichat.role.service.impl;

import com.codesuc.aichat.role.entity.Role;
import com.codesuc.aichat.role.mapper.RoleMapper;
import com.codesuc.aichat.role.service.RoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;
import java.util.List;

/**
* @Author: ZhangGongMing
* @CreateTime: 2025/09/23 12:56
* @Description: Role业务实现类
* @Version: 1.0
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
	private RoleMapper roleMapper;

    @Override
    public List<Role> getRoleList(String category) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果指定了分类，则按分类筛选
        if (StringUtils.hasText(category)) {
            queryWrapper.eq(Role::getCategory, category);
        }
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc(Role::getCreateTime);
        
        return this.list(queryWrapper);
    }

    @Override
    public Page<Role> getRoleListPage(Integer pageNo, Integer pageSize, String category) {
        Page<Role> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果指定了分类，则按分类筛选
        if (StringUtils.hasText(category)) {
            queryWrapper.eq(Role::getCategory, category);
        }
        
        // 按创建时间倒序排列
        queryWrapper.orderByDesc(Role::getCreateTime);
        
        return this.page(page, queryWrapper);
    }

}